package pcloud.client.worker;

import java.io.File;
import java.io.IOException;

import pcloud.client.util.FileResourceInfo;

public class DownloadTask extends CloudTask
{
	FileResourceInfo localFileInfo;
	File localFile;
	
	public DownloadTask(FileResourceInfo fileInfo)
	{
		super(fileInfo);
   		localFileInfo = fileInfo.toLocal();
	}
	
	@Override
	protected boolean doWork() throws IOException
	{
		localFile = fileService.downloadFile(fileInfo.getFullPath(), localFileInfo.getFullPath());
		if (localFile == null) {
			return false;
		}
		return true;
	}

	@Override
	protected boolean inTaskTriggerBeforeDoWork()
	{
		return true;
	}

	@Override
	protected boolean inTaskTriggerAfterDoWork()
	{
		localFileInfo.setLastModified(localFile.lastModified());
		localFileInfo.setLength(localFile.length());
		setFileInfo(localFileInfo);
		return true;
	}
	
	private void setFileInfo(FileResourceInfo targetFileInfo)
	{
		this.fileInfo = targetFileInfo;
	}

	@Override
	public String getBlockingFile()
	{
    	return localFileInfo.getFullPath();
 	}

}
